package com.service.single.example.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.service.single.entity.SystemUser;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 一休
 * @Date: 2025/2/10
 */
public class ExcelTemplate {

    public static void main(String[] args) throws IOException {
        // 准备数据
        List<SystemUser> users = new ArrayList<>();
        SystemUser user1 = new SystemUser();
        user1.setUsername("张三");
        user1.setNickname("zhangsan");
        user1.setPhone("11111");
        users.add(user1);
        SystemUser user2 = new SystemUser();
        user2.setUsername("张三");
        user2.setNickname("zhangsan");
        user2.setPhone("11111");
        users.add(user2);
        // 单个字段填充，使用map格式
        Map<String, Object> otherData = new HashMap<>();
        otherData.put("title", "系统用户表");
        // 模板文件路径
        String templateFileName = "src/main/resources/template.xlsx";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedDateTime = LocalDateTime.now().format(formatter);
        // 输出文件路径
        String outputFileName = "src/main/resources/filled_template_" + formattedDateTime + ".xlsx";
        // 输出文件路径
        String outputFileName1 = "src/main/resources/filled_template1_" + formattedDateTime + ".xlsx";

        // 1、直接通过文件模板填充
        try (ExcelWriter excelWriter = EasyExcel.write(outputFileName).withTemplate(templateFileName).build();) {
            // 创建写入工作表
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            // 每次使用 list 参数时创建新行。
            FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
            // 针对某个字段填充
            excelWriter.fill(otherData, fillConfig, writeSheet);
            // 填充集合
            excelWriter.fill(users, fillConfig, writeSheet);
        }


        // 2、通过IO流填充
        try (
                // 使用 try-with-resources 管理输入流
                InputStream inputStream = Files.newInputStream(Paths.get(templateFileName));
                InputStream templateInputStream = new BufferedInputStream(inputStream);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
        ) {
            try (
                    // 创建 ExcelWriter 对象
                    ExcelWriter excelWriter = EasyExcelFactory.write(outputStream).excelType(ExcelTypeEnum.XLSX).withTemplate(templateInputStream).build()
            ) {
                WriteSheet writeSheet = EasyExcelFactory.writerSheet().sheetNo(0).build();
                WriteSheet writeSheet1 = EasyExcelFactory.writerSheet().sheetNo(1).build();
                FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
                // 针对某个字段填充
                excelWriter.fill(otherData, fillConfig, writeSheet);
                // 填充集合
                excelWriter.fill(users, fillConfig, writeSheet);
                // 指定sheet填充数据
                excelWriter.fill(users, fillConfig, writeSheet1);
            }

            try (
                    FileOutputStream fileOutputStream = new FileOutputStream(outputFileName1)
            ) {
                outputStream.writeTo(fileOutputStream);
                // 刷新 FileOutputStream 缓冲区，确保数据全部写入文件
                fileOutputStream.flush();
                System.out.println("文件生成成功: " + outputFileName1);
            }
        }
    }
}

